package com.whc.gulimall.order.listener;

import com.rabbitmq.client.Channel;
import com.whc.common.to.mq.SecKillTO;
import com.whc.gulimall.order.constant.OrderMQConstant;
import com.whc.gulimall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestAttributes;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author angaoen
 */
@RabbitListener(queues = {OrderMQConstant.ORDER_SECKILL_ORDER_QUEUE})
@Slf4j
@Service
public class SecKillListener {

    @Resource
    private OrderService orderService;

    @RabbitHandler
    public void handleSecKill(SecKillTO to, Message message, Channel channel) throws IOException {
        log.info("处理秒杀商品：{}", to.toString());
        try {
            orderService.createKillOrder(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
        }
    }

}
